/*    file: TimestampChecker.cpp
 *    desc:
 * 
 * created: 2016-06-30
 *  author: chuanjiang.zh@qq.com
 * company: 
 */

#include "TimestampChecker.h"

namespace util
{


TimestampChecker::TimestampChecker():
    m_start(),
	m_last(),
    m_count()
{

}

void TimestampChecker::reset()
{
    m_start.reset();
    m_last.reset();
    m_count = 0;
}

int64_t TimestampChecker::input(int64_t clk, int64_t pts, Result* lastResult, Result* startResult)
{
	av::TimePoint curPoint(clk, pts);
	if (!m_start.isSet())
	{
		m_start = curPoint;
		m_last = curPoint;
	}

	m_count ++;

	int64_t offset = 0;
	if (lastResult)
	{
		offset = compute(curPoint, m_last, *lastResult);
	}

	if (startResult)
	{
		offset = compute(curPoint, m_start, *startResult);
	}

	m_last = curPoint;
	return offset;
}

int64_t TimestampChecker::getCount() const
{
    return m_count;
}

int64_t TimestampChecker::compute(const av::TimePoint& curPoint, const av::TimePoint& refPoint, Result& result)
{
	result.clockDiff = curPoint.m_clock - refPoint.m_clock;
	result.tsDiff = curPoint.m_pts - refPoint.m_pts;
	result.offset = result.tsDiff - result.clockDiff;
	return result.offset;
}


}
